package com.boost.ecoder;

import com.boost.util.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

/**
 * @author Santa
 * @since 2020/3/23
 */
public class HexEncoder extends MessageToMessageEncoder<String> {
    /**
     * Encode from one message to an other. This method will be called for each written message that can be handled
     * by this encoder.
     *
     * @param ctx the {@link ChannelHandlerContext} which this {@link MessageToMessageEncoder} belongs to
     * @param msg the message to encode to an other one
     * @param out the {@link List} into which the encoded msg should be added
     *            needs to do some kind of aggregation
     * @throws Exception is thrown if an error occurs
     */
    @Override
    protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
        byte[] bytes = ByteUtils.hexToByteArray(msg);
        System.out.println("发送命令"+ByteUtils.bytesToHexString(bytes));
        ByteBuf buf = Unpooled.wrappedBuffer(bytes);
        out.add(buf);
    }
}























